package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.response.DeleteMyErrorSubject
import com.chookss.tiku.response.PracticeSubjectList
import com.chookss.tools.MyEvent
import com.chookss.tools.Utils
import com.chookss.tools.httpForm
import org.greenrobot.eventbus.EventBus

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2020/12/30
 * Note: 错题本数据类
 */
class MyErrorSubjectViewModel() : BasePracticeTestViewModel() {
    private val pageSize = 50
    //题目列表
    val subjectList = mutableListOf<Subject>()
    lateinit var memoryLvl: String
    lateinit var subjectTypeCode: String
    lateinit var sourceType: String
    lateinit var total: String
    private fun createSubjectResult(subjectList: List<Subject>, listIndex: Int): LiveResult<Subject> {
        if (subjectList.size > listIndex) {
            return LiveResult(subjectList[listIndex])
        } else {//没有结果,结束了
            return LiveResult(null)
        }
    }

    private fun deleteSubjectCache(subject: Subject): Boolean {
        if (subjectList == null) {
            return false
        }
        for (i in (subjectList.size - 1) downTo 0) {
            if (subjectList[i].subjectCode == subject.subjectCode) {
                subjectList.removeAt(i)
                return true
            }
        }
        return false
    }

    //下载题目列表
    override fun getSubject(index: Int): LiveData<LiveResult<Subject>> {
        println(index)
        var liveResult = MutableLiveData<LiveResult<Subject>>()
        var pageNum = index / pageSize
        if(index!=0){
            if ( index< subjectList.size) {
                liveResult.postValue(createSubjectResult(subjectList, index))
                return liveResult
            }
        }
        val params2: MutableMap<String, Any?> = HashMap()
        if (!Utils.isNull(memoryLvl)) {
            params2["memoryLvl"] = memoryLvl
        }
        if (!Utils.isNull(subjectTypeCode)) {
            params2["subjectTypeCode"] = subjectTypeCode
        }
        if (!Utils.isNull(sourceType)) {
            params2["sourceType"] = sourceType
        }
        params2["pageNum"] = (pageNum + 1).toString()
        params2["pageSize"] =pageSize.toString()

        jobs + httpForm<PracticeSubjectList.Response> {
            url = "api/subject/getMyErrorSubject"
            params = params2
            onSuccess = {
                if(pageNum==0){
                    subjectList.clear()
                }
                total= it.total
                subjectList.addAll(it.data ?: emptyList())
                liveResult.postValue(createSubjectResult(subjectList, index))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }


    //提交答案
    override fun submitAnswer(subject: Subject, answers: List<String>, time: Long) = submitErrorSubject(subject, answers)

    fun deleteErrorSubject(subject: Subject): LiveData<LiveResult<DeleteMyErrorSubject.Result>> {
        var liveResult = MutableLiveData<LiveResult<DeleteMyErrorSubject.Result>>()
        jobs + httpForm<DeleteMyErrorSubject.Response> {
            url = "api/subject/deleteMyErrorSubject"
            params = mutableMapOf("subjectCode" to subject.subjectCode)
            onSuccess = {
                if (it.data?.rst == true){
                    liveResult.postValue(LiveResult(null))
                    deleteSubjectCache(subject)
                } else {
                    liveResult.postValue(LiveResult(Error("删除失败")))
                }
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }

//    fun getSameDayPracticeNum(): LiveData<LiveResult<GetSameDayPracticeNum.Result>> {
//        var liveResult = MutableLiveData<LiveResult<GetSameDayPracticeNum.Result>>()
//
//        jobs + httpForm<GetSameDayPracticeNum.Response> {
//            url = "api/practice/getSameDayPracticeNum"
//            params = mutableMapOf("practiceType" to practiceType)
//            onSuccess = {
//                liveResult.postValue(LiveResult(it.data))
//            }
//            onError = {
//                liveResult.postValue(LiveResult(Error(it)))
//            }
//        }
//        return liveResult
//    }
}
